<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
//is the user allowed to be here?
if(!fr.upmc.utils.SessionTracker.isUserConnected(request))
{
	fr.upmc.utils.SessionTracker.redirectToHome(response);
	return;
}

org.hibernate.Session s = fr.upmc.utils.HibernateUtils.getSessionFactory().openSession();
long id = -1;
try{
	id = Long.parseLong(request.getParameter("id"));
}
catch(Exception e){}

fr.upmc.dao.ContactDAO dao = new fr.upmc.dao.ContactDAO(s);
fr.upmc.bean.Contact contact;

contact = dao.read(id);

/*if(contact == null){
	response.sendRedirect("accueil.jsp");
	return;
}*/
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="scripts/login/css/style.css" />
    <script src="scripts/jquery/js/jquery-1.6.2.min.js"></script>
    <script src="scripts/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
    <script src="scripts/customInput.jquery.js"></script>
    <script src="scripts/login/js/login.js"></script>
    <script src="scripts/jquery/jquery.validate.js"></script>
    <link rel="stylesheet" href="scripts/jquery/css/ui-darkness/jquery-ui-1.8.16.custom.css" />
    <link rel="stylesheet" href="scripts/customInput.css" />
<title>Delete contact</title>

<script type="text/javascript">
$(document).ready(function(){
    var button = $('#logoutButton');
    var form = $('#logoutForm');
    
    button.click(function(){
    	if(confirm('Are you sure you want to logout?'))
    		form.submit();
    	else
    		return;
    });
    
    $('input:submit').button();
    $('input').customInput();
    
    $('#cancel').click(function(){
    	window.location = "accueil.jsp";
    	return false;
    });
    
    $('#delete').click(function(){
    	var q = "contactId=" + <%= id%>;
    	$("#dialog-modal").html('<p><img src="scripts/ajax-loader.gif"/></p>');
		setTimeout(function(){$.ajax({  
			  type: "POST",  
			  url: "deleteContact.do",  
			  data: q,  
			  success: function(data) {
				  if(data == "success"){
    				  $( "#dialog-modal" ).text("Contact deleted with success, redirecting in 2 seconds..");
    				  setTimeout(function(){
    					  window.location="accueil.jsp";
    					}, 2000);
				  }
				  else{
					  	$( "#dialog-modal" ).fadeIn('slow');
				  		$( "#dialog-modal" ).html("<h3>An error accured, please submit your data again</h3>");
				  }
			  }
			})}, 1000);
		
		$("#dialog-modal").dialog({
			height: 140,
			modal: true
		});
	
	return false;  
    });
});
</script>
</head>
<body>
<div id="bar">
        <div id="container">
            <!-- Login Starts Here -->
            <div id="title">MDOC Project - Delete Contact</div>
            <div id="loginContainer">
            	<a href="accueil.jsp" id="home">Home</a>
                <a href="#" id="logoutButton"><span>Log out</span></a>
                <div style="clear:both"></div>
                <div id="loginBox">                
                    <form id="logoutForm" action="authentification.do" method="post">
                        <fieldset id="body">
                            <input type="hidden" name="action" value="logout" >
                            <input type="submit" id="logout" value="Sign out" />
                        </fieldset>
                    </form>
                </div>
            </div>
            <!-- Login Ends Here -->
		</div>
	</div>
	
<div class="container">
	<br/>
	<%if(contact == null){%>
		Please <b><a href="searchContact.jsp">search</a></b> for a contact to delete first.
	<%}else{%>
		<form name="deleteContact" action="deleteContact.do" method="post">
			<fieldset>
				<legend>Confirm removal of contact</legend>
					<b>Contact id</b> : <%=contact.getContactId() %><br/>
					<b>Firstname</b> : <%=contact.getFirstName() %><br/>
					<b>Lastname</b>  : <%=contact.getLastName() %><br/>
					<b>Email</b> : <%=contact.getEmail() %>
			</fieldset>
			<br/>
			<input type="submit" name="Delete" id="delete" value="Delete"/>&nbsp;&nbsp;&nbsp;&nbsp; <input type="submit" name="Cancel" id="cancel" value="Cancel"/>
		</form>
	<%} %>
	<div id="dialog-modal" title="Deleting contact ..">
		<p></p>
	</div>
</div>

</body>
</html>